Page({
  data: {
    targetLow: 3.9,
    targetHigh: 7.0,
    reminderEnabled: false,
    reminderTime: '08:00',
    remindDays: ['周一', '周三', '周五']
  },
  
  onLoad() {
    // 加载设置
    this.loadSettings()
  },
  
  loadSettings() {
    try {
      const settings = wx.getStorageSync('glucoseSettings')
      if (settings) {
        this.setData(settings)
      }
    } catch (e) {
      console.error('加载血糖设置失败:', e)
    }
  },
  
  saveSettings() {
    try {
      wx.setStorageSync('glucoseSettings', {
        targetLow: this.data.targetLow,
        targetHigh: this.data.targetHigh,
        reminderEnabled: this.data.reminderEnabled,
        reminderTime: this.data.reminderTime,
        remindDays: this.data.remindDays
      })
      wx.showToast({
        title: '设置已保存',
        icon: 'success'
      })
    } catch (e) {
      console.error('保存血糖设置失败:', e)
      wx.showToast({
        title: '保存失败',
        icon: 'none'
      })
    }
  },
  
  onLowChange(e) {
    this.setData({
      targetLow: parseFloat(e.detail.value)
    })
  },
  
  onHighChange(e) {
    this.setData({
      targetHigh: parseFloat(e.detail.value)
    })
  },
  
  toggleReminder(e) {
    this.setData({
      reminderEnabled: e.detail.value
    })
  },
  
  onTimeChange(e) {
    this.setData({
      reminderTime: e.detail.value
    })
  },
  
  toggleDay(e) {
    const day = e.currentTarget.dataset.day
    const remindDays = [...this.data.remindDays]
    
    const index = remindDays.indexOf(day)
    if (index > -1) {
      remindDays.splice(index, 1)
    } else {
      remindDays.push(day)
    }
    
    this.setData({ remindDays })
  },
  
  onSave() {
    this.saveSettings()
  }
}) 